from src.llamafactory.hparams import read_args, get_ray_args, get_train_args
from transformers import HfArgumentParser
from src.llamafactory.hparams import ModelArguments, DataArguments, TrainingArguments, FinetuningArguments, \
    GeneratingArguments


# print(
#     ray_args)  # RayArguments(ray_run_name=None, ray_storage_path='./saves', ray_num_workers=1, resources_per_worker={'GPU': 1}, placement_strategy='PACK')

def _parse_args(
        parser, args=None, allow_extra_keys: bool = False
):
    args = read_args(args)
    if isinstance(args, dict):
        return parser.parse_dict(args, allow_extra_keys=allow_extra_keys)

    (*parsed_args, unknown_args) = parser.parse_args_into_dataclasses(args=args, return_remaining_strings=True)

    if unknown_args and not allow_extra_keys:
        print(parser.format_help())
        print(f"Got unknown args, potentially deprecated arguments: {unknown_args}")
        raise ValueError(f"Some specified arguments are not used by the HfArgumentParser: {unknown_args}")

    return tuple(parsed_args)


_TRAIN_ARGS = [ModelArguments, DataArguments, TrainingArguments, FinetuningArguments, GeneratingArguments]


def _parse_train_args(args):
    parser = HfArgumentParser(_TRAIN_ARGS)
    allow_extra_keys = True
    return _parse_args(parser, args, allow_extra_keys=allow_extra_keys)


args = read_args()
# 读取ray配置的训练参数
ray_args = get_ray_args(args)
print(ray_args,"ray_args")
# print(_parse_train_args(args))

model_args, data_args, training_args, finetuning_args, generating_args = get_train_args(args)
print("模型参数")
print(model_args)
print("数据参数")
print(data_args)
print("训练参数")
print(training_args)
print("微调参数")
print(finetuning_args)
print("生成参数")
print(generating_args)
"""
模型参数
ModelArguments(vllm_maxlen=4096, vllm_gpu_util=0.9, vllm_enforce_eager=False, vllm_max_lora_rank=32, vllm_config=None, export_dir=None, export_size=5, export_device='cpu', export_quantization_bit=None, export_quantization_dataset=None, export_quantization_nsamples=128, export_quantization_maxlen=1024, export_legacy_format=False, export_hub_model_id=None, image_max_pixels=589824, image_min_pixels=1024, video_max_pixels=65536, video_min_pixels=256, video_fps=2.0, video_maxlen=128, quantization_method='bitsandbytes', quantization_bit=None, quantization_type='nf4', double_quantization=True, quantization_device_map=None, model_name_or_path='meta-llama/Meta-Llama-3-8B-Instruct', adapter_name_or_path=None, adapter_folder=None, cache_dir=None, use_fast_tokenizer=True, 
resize_vocab=False, # 如果增加了特殊字符,而不是替换,需要设置为True
split_special_tokens=False, # 是否将特殊字符拆分为单独的token
new_special_tokens=None, model_revision='main', low_cpu_mem_usage=True, rope_scaling=None, flash_attn='auto', shift_attn=False, mixture_of_depths=None, use_unsloth=False, use_unsloth_gc=False, enable_liger_kernel=False, moe_aux_loss_coef=None, disable_gradient_checkpointing=False, use_reentrant_gc=True, upcast_layernorm=False, upcast_lmhead_output=False, train_from_scratch=False, infer_backend='huggingface', offload_folder='offload', use_cache=True, infer_dtype='auto', hf_hub_token=None, ms_hub_token=None, om_hub_token=None, print_param_status=False, trust_remote_code=True, compute_dtype=torch.bfloat16, device_map={'': device(type='cpu')}, model_max_length=2048, block_diag_attn=False)
数据参数
DataArguments(
template='llama3',  # 数据模版
dataset=['identity', 'alpaca_en_demo'], 
eval_dataset=None, 
dataset_dir='data', 
media_dir='data', 
cutoff_len=2048, 
train_on_prompt=False, 
mask_history=False, 
streaming=False, 
buffer_size=16384, 
mix_strategy='concat', 
interleave_probs=None, 
overwrite_cache=True, 
preprocessing_batch_size=1000, 
preprocessing_num_workers=16, 
max_samples=1000, 
eval_num_beams=None, 
ignore_pad_token_for_loss=True, 
val_size=0.0, 
packing=False, 
neat_packing=False, 
tool_format=None, 
tokenized_path=None)
训练参数
TrainingArguments(
_n_gpu=0,
accelerator_config={'split_batches': False, 'dispatch_batches': None, 'even_batches': True, 'use_seedable_sampler': True, 'non_blocking': False, 'gradient_accumulation_kwargs': None},
adafactor=False,
adam_beta1=0.9,
adam_beta2=0.999,
adam_epsilon=1e-08,
auto_find_batch_size=False,
batch_eval_metrics=False,
bf16=True,
bf16_full_eval=False,
data_seed=None,
dataloader_drop_last=False,
dataloader_num_workers=0,
dataloader_persistent_workers=False,
dataloader_pin_memory=True,
dataloader_prefetch_factor=None,
ddp_backend=None,
ddp_broadcast_buffers=None,
ddp_bucket_cap_mb=None,
ddp_find_unused_parameters=None,
ddp_timeout=180000000,
debug=[],
deepspeed=None,
disable_tqdm=False,
dispatch_batches=None,
do_eval=False,
do_predict=False,
do_train=True,
eval_accumulation_steps=None,
eval_delay=0, # 评估延迟时间,单位为秒
eval_do_concat_batches=True,
eval_steps=None,
eval_strategy=IntervalStrategy.NO, # 评估策略,IntervalStrategy.NO 不进行评估,IntervalStrategy.EPOCH 按照Epoch进行评估,IntervalStrategy.STEPS 按照步数进行评估
evaluation_strategy=None,
fp16=False,
fp16_backend=auto,
fp16_full_eval=False,
fp16_opt_level=O1,
fsdp=[],
fsdp_config={'min_num_params': 0, 'xla': False, 'xla_fsdp_v2': False, 'xla_fsdp_grad_ckpt': False},
fsdp_min_num_params=0,
fsdp_transformer_layer_cls_to_wrap=None,
full_determinism=False,
generation_config=None,
generation_max_length=None,
generation_num_beams=None,
gradient_accumulation_steps=8,
gradient_checkpointing=False,
gradient_checkpointing_kwargs=None,
greater_is_better=None, # 评估指标设定是否越大越好,check_metric_value 方法解释了这个参数的作用
group_by_length=False,
half_precision_backend=auto,
hub_always_push=False,
hub_model_id=None,
hub_private_repo=False,
hub_strategy=HubStrategy.EVERY_SAVE,
hub_token=<HUB_TOKEN>,
ignore_data_skip=False,
include_inputs_for_metrics=False,
include_num_input_tokens_seen=False,
include_tokens_per_second=False,
jit_mode_eval=False,
label_names=None,
label_smoothing_factor=0.0,
learning_rate=0.0001,
length_column_name=length,
load_best_model_at_end=False, # 是否在训练结束时加载最佳模型
local_rank=0,
log_level=passive,
log_level_replica=warning,
log_on_each_node=True,
logging_dir=saves/llama3-8b/lora/sft/runs/Jul17_10-13-00_Dyf-PC,
logging_first_step=False,  # 启用第一步日志记录
logging_nan_inf_filter=True,
logging_steps=10, # 日志记录的步数,每10步记录一次日志
logging_strategy=IntervalStrategy.STEPS, # 按照步数进行日志记录,还有其他两种 IntervalStrategy.EPOCH 按照epoch进行日志记录,IntervalStrategy.NO 不进行日志记录
lr_scheduler_kwargs={},
lr_scheduler_type=SchedulerType.COSINE,
max_grad_norm=1.0,
max_steps=-1,
metric_for_best_model=None, # 最佳模型的评估指标
mp_parameters=,
neftune_noise_alpha=None,
no_cuda=False,
num_train_epochs=3.0,
optim=OptimizerNames.ADAMW_TORCH,
optim_args=None,
optim_target_modules=None,
output_dir=saves/llama3-8b/lora/sft, # 对外输出的目录,模型会被保存到这个目录下,还会保存一些日志文件等
overwrite_output_dir=True, # 是否覆盖输出目录,如果为True,则会覆盖输出目录,如果为False,则会在输出目录下创建一个新的目录
past_index=-1,
per_device_eval_batch_size=8,
per_device_train_batch_size=1,
placement_strategy=PACK,
predict_with_generate=False,
prediction_loss_only=False,
push_to_hub=False,
push_to_hub_model_id=None,
push_to_hub_organization=None,
push_to_hub_token=<PUSH_TO_HUB_TOKEN>,
ray_num_workers=1,
ray_run_name=None,
ray_scope=last,
ray_storage_path=./saves,
remove_unused_columns=True,
report_to=[],
resources_per_worker={'GPU': 1},
restore_callback_states_from_checkpoint=False,
resume_from_checkpoint=None,
run_name=saves/llama3-8b/lora/sft,
save_on_each_node=False,
save_only_model=False,
save_safetensors=True,
save_steps=500, # 保存模型的步数,每500步保存一次模型
save_strategy=IntervalStrategy.STEPS, # 保存模型的方式,按照步数或者Epoch
save_total_limit=None,
seed=42,
skip_memory_metrics=True,
sortish_sampler=False,
split_batches=None,
tf32=None,
torch_compile=False,
torch_compile_backend=None,
torch_compile_mode=None,
torchdynamo=None,
tpu_metrics_debug=False,
tpu_num_cores=None,
use_cpu=False,
use_ipex=False,
use_legacy_prediction_loop=False,
use_mps_device=False,
warmup_ratio=0.1,
warmup_steps=0,
weight_decay=0.0,
)
微调参数
FinetuningArguments(
use_swanlab=False,  #  是否启用 SwanLab 跟踪 # https://blog.csdn.net/qq_45258632/article/details/144971398
swanlab_project='llamafactory', 
swanlab_workspace=None, 
swanlab_run_name=None, 
swanlab_mode='cloud', 
swanlab_api_key=None, # swanlab 需要 api_key
use_badam=False, 
badam_mode='layer', 
badam_start_block=None, 
badam_switch_mode='ascending', 
badam_switch_interval=50, 
badam_update_ratio=0.05, 
badam_mask_mode='adjacent', 
badam_verbose=0, 
use_apollo=False, 
apollo_target=['all'], 
apollo_rank=16, 
apollo_update_interval=200, 
apollo_scale=32.0, 
apollo_proj='random', 
apollo_proj_type='std', 
apollo_scale_type='channel', 
apollo_layerwise=False, 
apollo_scale_front=False, 
use_galore=False, 
galore_target=['all'], 
galore_rank=16, 
galore_update_interval=200, 
galore_scale=2.0, 
galore_proj_type='std', 
galore_layerwise=False, 
pref_beta=0.1, 
pref_ftx=0.0, 
pref_loss='sigmoid', 
dpo_label_smoothing=0.0, 
kto_chosen_weight=1.0, 
kto_rejected_weight=1.0, 
simpo_gamma=0.5, 
ppo_buffer_size=1, 
ppo_epochs=4, 
ppo_score_norm=False, 
ppo_target=6.0, 
ppo_whiten_rewards=False, 
ref_model=None, 
ref_model_adapters=None, 
ref_model_quantization_bit=None, 
reward_model=None, 
reward_model_adapters=None, 
reward_model_quantization_bit=None, 
reward_model_type='lora', 
additional_target=None, 
lora_alpha=16, 
lora_dropout=0.0, 
lora_rank=8, 
lora_target=['all'], 
loraplus_lr_ratio=None, 
loraplus_lr_embedding=1e-06, 
use_rslora=False, # rsLoRA 微调方法 # https://zhuanlan.zhihu.com/p/24017770766
use_dora=False,  # DoRA 微调方法
pissa_init=False, # PiSSA 微调方法
pissa_iter=16,  # PiSSA 微调方法
pissa_convert=False, #  PiSSA 微调方法
create_new_adapter=False, 
freeze_trainable_layers=2, 
freeze_trainable_modules=['all'], 
freeze_extra_modules=None, 
pure_bf16=False, stage='sft', 
finetuning_type='lora', 
use_llama_pro=False, 
use_adam_mini=False, 
freeze_vision_tower=True, 
freeze_multi_modal_projector=True, 
train_mm_proj_only=False, 
compute_accuracy=False, 
disable_shuffling=False, 
plot_loss=True, 
include_effective_tokens_per_second=False)
生成参数
GeneratingArguments(
do_sample=True, 
temperature=0.95, 
top_p=0.7, 
top_k=50, 
num_beams=1, 
max_length=1024, 
max_new_tokens=1024, 
repetition_penalty=1.0, 
length_penalty=1.0, 
default_system=None, 
skip_special_tokens=True)
"""